﻿using Ldt.DataEngine.DataFlowParse;
using Ldt.DataEngine.DataFlowParse.Model;
using Ldt.DataEngine.LogHandle;
using Ldt.InformationUnit.Common;
using LdtJudge.DataEngine.CallInterface;
using LdtJudge.DataEngine.Common.CommHelp;
using LdtJudge.DataEngine.Common.XmlUtility;
using LdtJudge.DataEngine.Data.DataParseStorage;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.CommModel;
using LdtJudge.DataEngine.Model.DataModel.DataParseStorage;
using LdtJudge.DataEngine.Model.DataModel.SysCommon;
using LdtJudge.DataEngine.Model.OptimizationModel;
using LdtJudge.DataEngine.Model.XMLConfigModel.ElmentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static Ldt.DataEngine.LogHandle.LogHandle;

namespace Ldt.DataEngin.Assemble
{
    /// <summary>
    /// 回填信息处理
    /// </summary>
    public class FillBackDataModel
    {

        #region 基础信息整理
        private LogShow _uishow;
        private LogShowFontStyle _uilogShowFontStyle;
        public IConfigRetrieval configRetrieval;
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="logType">消息类型</param>
        /// <param name="text">日志内容</param>
        private void DoSetLogtype(string text, LogMessageType logType = LogMessageType.Warn)
        {
            LogHType log = LogHType.Info;
            foreach (LogHType typ in Enum.GetValues(typeof(LogHType)))
            {
                if (((int)logType) == ((int)typ))
                {
                    log = typ;
                    break;
                }
            }
            //写入记录日志
            LogHandle.GetInitLogHandle.LogWrite(log, text);
            if (_uishow != null)
            {
                this._uishow(text, logType);
            }


        }

        public int IsEvidence { get; set; }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="logText">日志内容</param>
        /// <param name="fontStyle">样式</param>
        /// <param name="logType">日志输出等级</param>
        private void DoSetLogStyle(string logText, FontStyles fontStyle, LogMessageType logType)
        {
            //DoSetLogtype(logType,logText);
            LogHType log = LogHType.Info;
            foreach (LogHType typ in Enum.GetValues(typeof(LogHType)))
            {
                if (((int)logType) == ((int)typ))
                {
                    log = typ;
                    break;
                }
            }
            //写入记录日志
            LogHandle.GetInitLogHandle.LogWrite(log, logText);
            if (_uilogShowFontStyle != null)
            {
                this._uilogShowFontStyle(logText, fontStyle);
            }


        }
        /// <summary>
        /// 日志显示
        /// </summary>
        public LogShow LogShow
        {
            get { return DoSetLogtype; }
            set { _uishow = value; }
        }
        public InteractiveShowClass InteractiveShowClass { get; set; }

        /// <summary>
        /// 显示带有样式的日志
        /// </summary>
        public LogShowFontStyle LogShowFontStyle
        {
            get { return DoSetLogStyle; }
            set { _uilogShowFontStyle = value; }
        }
        /// <summary>
        /// 总体进度显示
        /// </summary>
        public ProcessShow ProcessShow { get; set; }

        /// <summary>
        /// 模块进度显示
        /// </summary>
        public ModuleProcessShow ModuleProcessShow { get; set; }
        #endregion

        /// <summary>
        /// 回填信息处理声明基础变量
        /// </summary>
        /// <param name="casetype">案件类型编号</param>
        /// <param name="courtID">法院ID</param>
        /// <param name="sD_CaseOnlyID">案件唯一ID</param>
        /// <param name="appId">应用ID</param>
        /// <param name="docType">文书编号</param>
        /// <param name="anjjd">案件编号</param>
        /// <param name="msgid">消息ID</param>
        /// <param name="evidencePathList">证据路径</param>
        /// <param name="cCIDs">案由编号</param>
        public void FillBackData(List<DataContent> dataList,string casetype, string courtID, string sD_CaseOnlyID, string appId, string docType, string anjjd, string msgid, string evidencePathList, List<ConfigModel> configModels,string cCIDs,List<OredContent> oredContents)
        {
            MainInputParameterModel inputParameter = new MainInputParameterModel()
            {
                CaseType = casetype,
                DocID = docType,
                SourceContent = "",
                SD_CaseOnlyID = sD_CaseOnlyID,
                FilePath = "",
                CaseCause = "",
                CourtId = courtID,
                AppId = appId,
                CaseID = sD_CaseOnlyID,
                MsgID = msgid,
                laststage = anjjd,
                FL_ID = "",
                SF_ID = "",
                IsSave = true
            };

            #region 分析参数基础赋值
            ConfigFlowLoad configFlowLoad = new ConfigFlowLoad();
            DataEngin.Assemble.MainComponentBase main = new DataEngin.Assemble.MainComponentBase(configFlowLoad);
            main.inputParameter = inputParameter;
            main.LogShow = new LogShow(LogShow);
            main.ProcessShow = new ProcessShow(ProcessShow);
            main.ModuleProcessShow = new ModuleProcessShow(ModuleProcessShow);
            main.dataContext = new DataContext(new DataLayerSysStores(), new DyData_Analysis(), new InteractiveShowClass());
            main.dataContext.DataLayerAns.SatgeAnalysis = anjjd;
            main.dataContext.DataLayerAns.CaseType = casetype;
            main.dataContext.DataLayerAns.SourceCode = docType;
            main.dataContext.DataLayerAns.FengXiCanShu = new LdtJudge.DataEngine.Model.DataModel.DataParseStorage.An_fengxicanshu();
            #endregion
            if (string.IsNullOrEmpty(cCIDs))
            {
                DAL_An_anjianxinxi anjianxin = new DAL_An_anjianxinxi();
                An_anyouxinxi anyouxinxi = anjianxin.GetAnYouxinxiByAnjianid(inputParameter.CaseID);
                if (anyouxinxi != null)
                    cCIDs = anyouxinxi.Anyoucode;
            }
            // 案由信息
            if (!string.IsNullOrEmpty(cCIDs))
            {
                GetSysCaseCause dalCause = new GetSysCaseCause();
                foreach (var ccid in cCIDs.Split(','))
                {
                    CaseCauseModel MCause = new CaseCauseModel();
                    if (inputParameter.CaseType == "xzzf")
                    {
                        inputParameter.CaseCause = ccid;
                        MCause = dalCause.GetModelByCauseNamexzzf(ccid, inputParameter.CaseType);
                    }
                    else
                    {
                        MCause = dalCause.GetModelByCauseName(ccid, inputParameter.CaseType);
                    }
                    CaseCauseModel resultCause = null;
                    try
                    {
                        //进入案由指向性
                        Dictionary<string, string> dicElement = new Dictionary<string, string>();
                        dicElement.Add("fileFullName", @"config\moduleconfig\commonconfig\generalconfig\causepoint\1939.xml");
                        string elementMessage = WebUtil.DoGet(configRetrieval.GetAppSettingString("systemapi") + "/api/WordParse/GetConfigFileContent", dicElement, "utf-8");
                        dynamic elementcofig = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(elementMessage);
                        string configContent = "";
                        if (elementcofig.result.code.ToString() == "1")
                            configContent = elementcofig.body;

                        if (!string.IsNullOrEmpty(configContent))
                        {
                            CausePointModel causePointModel = XmlUtility.DeserializeToObject<CausePointModel>(configContent);
                            CaseCauseModel replaceCause = null;
                            if (causePointModel != null)
                            {

                                if (causePointModel.Recrods != null && causePointModel.Recrods.Any())
                                {
                                    GetSysCaseCause getSysCaseCause = new GetSysCaseCause();
                                    var record = causePointModel.Recrods.FirstOrDefault(x => x.Sourceccid == MCause.CC_ID);
                                    if (record != null)
                                    {
                                        replaceCause = getSysCaseCause.GetModel(record.Targetccid);
                                        MCause.AllParentId = replaceCause.CC_ID;
                                    }
                                    else
                                    {
                                        MCause.AllParentId = MCause.CC_ID;
                                    }
                                }

                            }
                            resultCause = replaceCause == null ? MCause : replaceCause;
                        }
                    }
                    catch (Exception) { }

                    if (main.dataContext.DataLayerAns.CaseCausesList == null)
                        main.dataContext.DataLayerAns.CaseCausesList = new List<CaseCauseModel>();
                    main.dataContext.DataLayerAns.CaseCausesList.Add(resultCause);
                    main.dataContext.DataLayerAns.ExtractCaseCausesList = new List<CaseCauseModel>();
                    main.dataContext.DataLayerAns.ExtractCaseCausesList.Add(MCause);
                    if (main.dataContext.DataLayerAns.CausePointDic == null)
                        main.dataContext.DataLayerAns.CausePointDic = new Dictionary<string, string>();
                    if (!main.dataContext.DataLayerAns.CausePointDic.ContainsKey(MCause.CC_ID))
                        main.dataContext.DataLayerAns.CausePointDic.Add(MCause.CC_ID, resultCause.CC_ID);
                }
            }
            #region  对上下文的基础变量做赋值，主要与输入参数inputParameter做对应
            main.dataContext.DataLayerAns.CaseType = inputParameter.CaseType;
            main.dataContext.DataLayerAns.CourtId = inputParameter.CourtId;
            main.dataContext.DataLayerAns.AppId = inputParameter.DocID;
            main.dataContext.DataLayerAns.MsgId = inputParameter.MsgID;
            main.dataContext.DataLayerAns.SF_ID = inputParameter.SF_ID;
            main.dataContext.DataLayerAns.FL_ID = inputParameter.FL_ID;
            main.dataContext.DataLayerAns.CaseId = inputParameter.CaseID;
            main.dataContext.DataLayerAns.SourceCode = inputParameter.DocID;
            main.dataContext.DataLayerAns.IsEvidence = IsEvidence;
            main.dataContext.DataLayerAns.EvidenceXpathList = evidencePathList;
            main.dataContext.DataCommon.CaseType = casetype;
            main.dataContext.DataCommon.SourceCode = docType;
            main.dataContext.DataCommon.MsgId = inputParameter.MsgID;
            main.dataContext.DataLayerAns.dataContentList = dataList;
            main.dataContext.DataLayerAns.OredContents = oredContents;
            #endregion
            if (configModels == null || !configModels.Any())
                main.GetFlowConfig(ConfigFlowLoad.FlowType.Opt);
            else
                main.configmodels = configModels;
            main.ExtractWrap();
        }
    }
}
